<template>
  <div class="imglist-container">
    <!-- 顶部滑动条区域 -->
    <div class="scroll-bar">
      <cube-scroll-nav-bar :current="current" :labels="labels" @change="changeHandler" />
    </div>

    <!-- 图片列表 -->
    <ul class="img-list">
      <li v-for="item in imagelist" :key="item.id" @click="goImageDetail(item.id)">
        <!-- 需要被懒加载的图片 -->
        <img v-lazy="item.img_url" />
        <!-- 图片之上的说明性文字 -->
        <div class="cover">
          <h1 class="title">{{ item.title }}</h1>
          <div class="summary">{{ item.zhaiyao }}</div>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  layout: 'cms',
  data() {
    return {
      current: '全部',
      imagelist: []
    }
  },
  computed: {
    // 头部滑动条的 label 元素
    labels() {
      return this.catelist.map((x) => {
        return x.title
      })
    }
  },
  // 异步获取页面数据
  async asyncData({ params, $axios }) {
    // 获取滑动条数据
    let catelist = []
    const { data: res } = await $axios.get('/api/getimgcategory')
    if (res.status === 0) {
      catelist = res.message
      catelist.unshift({ id: 0, title: '全部' })
    }

    // 获取图片列表数据
    let imagelist = []
    const { data: res2 } = await $axios.get(`/api/getimages/0`)
    if (res2.status === 0) {
      imagelist = res2.message
    }

    return {
      catelist,
      imagelist
    }
  },
  mounted() {
    this.getImageList(0)
  },
  // 事件处理函数
  methods: {
    // 滑动条选中项改变，会触发这个函数
    changeHandler(cur) {
      this.imagelist = []
      this.current = cur
      const id = this.catelist.filter((x) => {
        return x.title === cur
      })[0].id
      this.getImageList(id)
    },
    // 根据Id获取图片列表数据
    async getImageList(id) {
      const { data: res } = await this.$axios.get(`/api/getimages/${id}`)
      this.imagelist = res.message
    },
    // 通过路由，导航到图片详情页面
    goImageDetail(id) {
      this.$router.push('/shareimage/' + id)
    }
  }
}
</script>

<style lang="less" scoped>
.imglist-container {
  padding-top: 53.6px;
}
.scroll-bar {
  width: 100%;
  position: fixed;
  top: 40px;
  left: 0;
  z-index: 100;
}
.img-list {
  z-index: 0;
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  li {
    width: 95%;
    min-height: 356px;
    background-color: #aaa;
    position: relative;
    + li {
      margin-top: 10px;
    }
    img {
      // 图片的默认样式
      width: 100%;
      display: block;
    }
  }
}

.cover {
  position: absolute;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.4);
  color: white;
  max-height: 84px;
  overflow: hidden;
  .title {
    font-size: 13px;
    font-weight: bold;
    padding: 5px 0;
    text-align: center;
  }
  .summary {
    font-size: 12px;
    line-height: 20px;
    text-align: left;
    text-indent: 2em;
    padding: 0 3px;
  }
}
</style>
<style>
/* 加载中，loading图片的样式 */
img[lazy='loading'] {
  width: 40px !important;
  height: 300px;
  margin: auto;
}
/* 加载完成后，图片的样式 */
img[lazy='loaded'] {
  width: 100%;
  background-color: white;
  display: block;
}
</style>
